#include "lpr_infer_mnn_api.h"

DetectRecPlateMnn::DetectRecPlateMnn()
{
    recModel.Init(rec_model_path + ".mnn", rec_input_name, rec_output_name);
}

DetectRecPlateMnn::~DetectRecPlateMnn()
{
}

void DetectRecPlateMnn::get_plate_result(cv::Mat &img, std::string &plate_number, float &plate_char_conf,
                                         std::string &plate_color, std::vector<float>& char_conf)
{
    cv::resize(img, img, cv::Size(plate_rec_input_w, plate_rec_input_h));
    cv::Mat pr_img = img;
    // auto pre_time_b=cv::getTickCount();
    float *blob_rec = new float[plate_rec_input_w * plate_rec_input_h * 3];
    blobFromImage_plate(pr_img, mean_value, std_value, blob_rec);
    // auto pre_time_e=cv::getTickCount();
    // auto time_gap_pre = (pre_time_e-pre_time_b)/cv::getTickFrequency()*1000;
    // printf("识别算法预处理时间: %.2fms\n",time_gap_pre);

    // auto time_b = cv::getTickCount();
    recModel.doInference(blob_rec);
    // auto time_e = cv::getTickCount();
    // auto time_gap = (time_e-time_b)/cv::getTickFrequency()*1000;
    // printf("识别算法推理时间: %.2fms\n",time_gap);

    // auto time_b_decode = cv::getTickCount();
    decode_outputs(recModel.prob, recModel.output_size - 1, plate_number, plate_char_conf, char_conf);  // 最后一个输出为车牌颜色
    // plate_color = decode_color_outputs(recModel.prob,recModel.output_size);

    delete[] blob_rec;
    // auto time_e_decode = cv::getTickCount();
    // auto time_gap_decode = (time_e_decode-time_b_decode)/cv::getTickFrequency()*1000;
    // printf("识别算法后处理时间: %.2fms\n",time_gap_decode);
}

void DetectRecPlateMnn::infer(cv::Mat &img, std::string &plate_number, float &plate_char_conf, std::string &plate_color,
                              std::vector<float>& char_conf)
{
    get_plate_result(img, plate_number, plate_char_conf, plate_color, char_conf);
}
